:- module(_,_,[make,functions]).
:- use_module(library('make/system_extra')).
:- use_module(library(lists),[append/3]).
:- use_module(library(terms),[atom_concat/2]).
:- use_module(library(format), [format/2, format/3]).
:- use_module(library(random), [random/3]).
:- use_module('SETTINGS').


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Look at the SETTINGS.pl file in this directory and edit it if necessary     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This Makefile *needs* Ciao lpmake

% lpmake install:        compile and install the whole WebDB system

% lpmake justinstall:   install the whole WebDB system

% lpmake compile:        compile WebDB sources

% lpmake clean:          delete compilation auxiliary files

% lpmake distclean:      delete all files which can be generated automatically 

% lpmake maintenance:    generate WebDB frontpages

% lpmake show_settings:  show current settings

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INSTALLATION                                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

install <- [install_header,show_settings,make_dirs,just_compile,do_install] :-
	true.

just_install <- [install_header, show_settings, do_install] :-
	true.

do_install <- :-
% Installing compiled sources
 	do(['cd src ','; ', ~lpmake, ' install'],nofail),
% Setting right permissions for user and admin frontapages	
	do(['chgrp -R ',~dbserver_group,' ',~dbhome,'/*.html'], nofail),
% Setting right permissions for data and desc persistent files
% Desc:
	atom_concat(~dbdesc, '/dbserver', DBDSE),
	do(['chgrp -R ',~dbserver_group,' ',DBDSE], nofail).
%	do(['cd ', DBDSE,' ; chmod u+w *'], nofail),
%Data:
       atom_concat(~dbdata, '/dbserver', DB),
       do(['chgrp -R ',~dbserver_group,' ',DB], nofail),
	newline(NL),
	display('WebDB files installed'),nl,
	display(NL), nl,
	display('WebDB installation finished. Enjoy!'), nl,
	display(NL), nl.

install_header <- :-
	newline(NL),
	display(NL), nl,
	display('Installing WebDB'), nl,
	display(NL), nl.

show_settings <- :- 
	show_settings1,
	newline(NL),
	display(NL), nl, nl.

make_dirs <- [make_home, make_admin, make_desc, make_data, make_location,
	       make_types, make_templates, make_add, make_pics,
	       make_graph] :- true.

make_home <- :-
	display('Creating WebDB file structure'), nl, nl,
	display('Creating web site home'), nl, 
	make_dirpath(~dbhome),
	do(['/bin/cp README.html ', ~dbhome], nofail).

make_admin <- :-
	display('Creating administration web site'), nl, 
        make_dirpath(~dbadmin),
	set_perms(~dbadmin, ~webdirperms),
	do(['chgrp ',~dbserver_group,' ',~dbadmin], nofail),
 	atom_concat(~dblog_dir, '/dbserver.log', DBLog),
 	writef("# dbserver log file", DBLog),
        set_perms(DBLog, ~dbserverlogperms).

make_desc <- :-
	display('Installing database description files'), nl,
	atom_concat(~dbdesc, '/dbserver', DBDSE),
	make_dirpath(DBDSE),
	set_perms(DBDSE, ~webdirperms),
	do(['/bin/cp  db_desc_example/dbserver/* ', DBDSE], nofail).	

make_data <- :-
	display('Installing database files'), nl,
	atom_concat(~dbdata, '/dbserver', DB),
	make_dirpath(DB),
	set_perms(DB, ~webdirperms).

make_location <- :-
	make_dirpath(~dblocation),
	set_perms(~dblocation, ~webdirperms),
	do(['chgrp -R ',~dbserver_group,' ',~dblocation], nofail).

make_types <- :-
	display('Installing database type files'), nl,
	make_dirpath(~dbtypes),
	set_perms(~dbtypes, ~webexeperms),
	do(['/bin/cp db_types_example/* ', ~dbtypes], nofail).

make_templates <- :-
	display('Installing HTML templates'), nl,
	make_dirpath(~dbtemplates),
	set_perms(~dbtemplates, ~webdirperms),
	do(['/bin/cp html_templates/*.html ', ~dbtemplates], nofail),
	do(['/bin/cp ', ~amos_templates, '/*.html ',  ~dbtemplates], nofail),
	do(['chgrp -R ',~dbserver_group,' ',~dbtemplates], nofail).

make_add <- :-
	display('Installing networking infrastructure'), nl,
	make_dirpath(~dbadd),
        set_perms(~dbadd, ~webdirperms),
	do(['chgrp ', ~dbserver_group,' ',~dbadd], nofail).

make_pics <- :-
	display('Installing web site images'), nl,
	atom_concat(~dbhome, '/pictures', Pics),
 	make_dirpath(Pics),
	do(['/bin/cp pictures/* ', Pics], nofail).

make_graph <- :-
	display('Installing additional utilities'), nl,
	do(['/bin/cp utils/graph ', ~dbadmin], nofail),
	atom_concat(~dbadmin, '/graph', Graph),
	set_perms(Graph, ~webexeperms).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% COMPILATION                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

compile <- [make_dirs, just_compile] :- true.
just_compile <- :-
	display('Compiling WebDB sources: '), nl, nl,
 	do(['cd src ','; ', ~lpmake, ' compile'],nofail),
	display('Compilation finished'), nl,
	newline(NL),
	display(NL), nl.	

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CLEAN                                                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clean <- :-
	delete_files(~ls('*.po|*.itf|tmp*|*~')),
	do(['cd src ','; ', ~lpmake, ' clean'],nofail),
	newline(NL),
	display(NL), nl,
	display('Deleted files *.po, *.itf, tmp*, *~'),nl.

distclean <- [clean] :-
	do(['cd src ','; ', ~lpmake, ' distclean'],nofail).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAINTENANCE                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

maintenance <- :-
	do(['cd src ','; ', ~lpmake, ' maintenance'], nofail).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SHOW CONFIGURATION                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
show_settings1 :-
	display('Settings specified in SETTINGS.pl: '), nl, nl,
	display('Operating System type: '),
	display(~os), nl,
	display('Ciao compiler: '), 
	display(~ciaoc),nl,
	display('Database server address published in: '),
	display(~dbadd), nl,
	display('WebDB home: '),
	display(~dbhome), nl,
	display('WebDB admin directory: '),
	display(~dbadmin), nl,
	display('WebDB URL: '),
	display(~dburl), nl,
	display('WebDB base directory: '),
	display(~dbdatadesc), nl,
	display('WebDB templates :'),
	display(~dbtemplates), nl,
	display('WebDB database: '),
	display(~dbdata), nl,
	display('WebDB database description: '),
	display(~dbdesc), nl,
	display('AMOS database location: '),
	display(~dblocation), nl,
	display('WebDB database types: '),
	display(~dbtypes), nl.
	
newline('------------------------------------------------------------------'). 
